#include "Login.h"
#include "Database.h"
#ifndef DATABASE_FILE
#define DATABASE_FILE "Database.Assn2"

Login::Login() {
    loginTries = 0;
    loginIndex = 0;
}

int Login::checkValidUserPass(string id, string pass) {
    Login LA;
    int count = 0;
    
    Database *db;
    db = new Database("Database.Assn2");
    vector<vector<string> > result = db->query("SELECT * FROM users;");
    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it)
    {
        count++;
        vector<string> row = *it;
            if(id == row.at(1)){
                if(row.at(4) != "locked"){
                    if(pass == row.at(2)){
                        cout << "Successfully log in" << endl << endl;
                        loginIndex = count;
                        userID = row.at(0);
                        username = row.at(1);
                        role = row.at(3);
                        return 1;   
                    } else {
                        cout << "Invalid Password" << endl << endl;
                        return -1;
                    }
                }else if (row.at(4) == "locked"){
                cout << "Your account is locked!" << endl;
                cout << "Contact the admin to get it unlocked" << endl;
                cout << "Exiting Student Attedance System!" << endl << endl;
                return 0;
                }
            }
    }
    cout << "Invalid ID" << endl << endl;
    return -1;
    db->close();
    
}

bool checkPassword(string pass) {
    
    //Check if there are 8 chars
    if (pass.length() < 8) {
        cout << endl << "New Password has to be at least 8 chars long." << endl;
        return false;
    }

    int numCount = 0;
    int alphaCount = 0;

    //Go through every character in password and count num of alphabet and num
    for (int i = 0; i < pass.length(); i++) {
        if (isalpha(pass[i])) {
            alphaCount++;
        } else if (isdigit(pass[i])) {
            numCount++;
        }
    }
    //If there is at least 1 alpha and num
    if (numCount > 0 && alphaCount > 0) {
        cout << endl << "New password valid." << endl;
        return true;
    }

    //If all else fails means, it did not contain 1 num and 1 alpha
    cout << endl << "New password has to contain 1 number and 1 alpha" << endl;
    return false;
}

void Login::changePassword() {
    string password;
    cout << endl << "Proceeding to Change Password....." << endl;
    while (1) { // keeps looping, only exits when password updated
        cout << "Please enter new password: ";
        cin >> password;
        cin.clear();
        cin.ignore(100, '\n');
        //Check if password is valid
            if (checkPassword(password)) {
                cout << "Updating Password..." << endl;
                
                Database *db;
                db = new Database("Database.Assn2");
                
                string sqlstatement;
                sqlite3 *db1;
                sqlite3_stmt *stmt;
                vector<vector<string> > result = db->query("SELECT * FROM users;");
                for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it)
                {
                    vector<string> row = *it;   
                    if(username == row.at(1)){
                        sqlstatement = "UPDATE users SET password='"+password+"' WHERE username='"+username+"'";                     
                       
                        if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                        {
                            sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                            sqlite3_step(stmt);
                            sqlite3_finalize(stmt);
                            sqlite3_close(db1);
                        }
                
                       }
                }
           return;
           }        
     }  
}

int Login::getLogin() {
    
    string id, pass;
    cout << "Please enter login ID and Password.." << endl;
    cout << "Login ID: ";
    cin >> id;
    cin.clear();
    cin.ignore(100, '\n');

    cout << "Password: ";
    cin >> pass;
    cin.clear();
    cin.ignore(100, '\n');

    return checkValidUserPass(id, pass); //return -1 for failure, 1 for success
}
void Login::adminInterface() {
    Login LA;

    while (1) {
        cout << "<<<<<<< Student Attendance System >>>>>>>" << endl;
        cout << "<<<<<<<      Admin Interface      >>>>>>>" << endl << endl;
    
            int choice = 0;
            cout << "User Management" << endl;
            cout << "   1) Create user" << endl;
            cout << "   2) Edit user" << endl;
            cout << "   3) Delete user" << endl;
            cout << "   4) Unlock user" << endl;
            cout << "Attendance Management" << endl;
            cout << "   5) Create module" << endl;
            cout << "   6) Edit module" << endl;
            cout << "   7) Delete module" << endl;
            cout << "   8) Assign Lab/Lecture/Tutorial" << endl;
            cout << "   9) Display all classes and lecturers" << endl;
            cout << "   10) Exit Program" << endl;
            cout << "   Please enter your choice: ";
            cin >> choice;
            cin.clear();
            cin.ignore(100, '\n'); 
            
            if (choice == 1){
                LA.adminCreateUser();
            } else if (choice == 2){
                LA.adminEditUser();
            } else if (choice == 3){
                LA.adminDeleteUser();
            } else if (choice == 4){
                LA.adminUnlockUser();
            } else if (choice == 5){
                LA.adminCreateModule();
            } else if (choice == 6){
                LA.adminEditModule();
            } else if (choice == 7){
                LA.adminDeleteModule();
            } else if (choice == 8){
                LA.adminAssign();
            } else if (choice == 9){
                LA.adminDisplayAll();
            } else if (choice == 10){
                cout << "Exiting Program!!!" << endl;
                exit(0);
            }
    }
}

void Login::adminCreateUser(){
   string newuser, newpass, newrole, newfirst, newlast, newemail;
   int roleChoice;
   Login LA;
        cout << endl << "Proceeding to create user....." << endl;
        cout << "Please enter username: ";
        cin >> newuser;
        cin.clear();
        cin.ignore(100, '\n');
        cout << "Please enter password: ";
        cin >> newpass;
        cin.clear();
        cin.ignore(100, '\n');
        if (checkPassword(newpass)){
            cout << "Please enter first name: ";
            cin >> newfirst;
            cin.clear();
            cin.ignore(100, '\n');
            cout << "Please enter last name: ";
            cin >> newlast;
            cin.clear();
            cin.ignore(100, '\n');
            cout << "Please enter email: ";
            cin >> newemail;
            cin.clear();
            cin.ignore(100, '\n');
            cout << "Please select role of user" << endl;
            cout << "       1) Admin" << endl;
            cout << "       2) Lecturer" << endl;
            cout << "       3) Student" << endl;
            cout << "       Enter your choice: ";
            cin >> roleChoice;
            if (roleChoice == 1 || roleChoice == 2 || roleChoice == 3){
                if (roleChoice == 1)
                    newrole = "admin";
                if (roleChoice == 2)
                    newrole = "lecturer";
                if (roleChoice == 3)
                    newrole = "student";
                cin.clear();
                cin.ignore(100, '\n');
                
                if (newrole == "admin"){
                    Database *db;
                    db = new Database("Database.Assn2");
                    
                    string sqlstatement;
                    sqlite3 *db1;
                    sqlite3_stmt *stmt;
                    
                    sqlstatement = "INSERT INTO users (userName, password, role, accountStatus) VALUES ('"+newuser+"','"+newpass+"','"+newrole+"','unlocked')";
                    if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK){
                    sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                    sqlite3_step(stmt);
                    }
                    
                    vector<vector<string> > result = db->query("SELECT * FROM users;");
                    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it){
                        
                        vector<string> row = *it;   
                        string tempID;
                        if (newuser == row.at(1)){
                            
                            tempID = row.at(0);
                        
                            sqlstatement = "INSERT INTO admin VALUES ('"+tempID+"','"+newuser+"','"+newfirst+"','"+newlast+"','"+newemail+"')";
                            if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK){
                                sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                                sqlite3_step(stmt);
                                sqlite3_finalize(stmt);
                                sqlite3_close(db1);
                            }
                        } 
                    }
                    db->close();
                    cout << "New " << newrole << " " << newuser << " successfully added!" << endl;
                    return;
                } else  if (newrole == "lecturer"){
                    Database *db;
                    db = new Database("Database.Assn2");
                    
                    string sqlstatement;
                    sqlite3 *db1;
                    sqlite3_stmt *stmt;
                    
                    sqlstatement = "INSERT INTO users (userName, password, role, accountStatus) VALUES ('"+newuser+"','"+newpass+"','"+newrole+"','unlocked')";
                    if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK){
                    sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                    sqlite3_step(stmt);
                    }
                    
                    vector<vector<string> > result = db->query("SELECT * FROM users;");
                    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it){
                        
                        vector<string> row = *it;   
                        string tempID;
                        if (newuser == row.at(1)){
                            
                            tempID = row.at(0);
                        
                            sqlstatement = "INSERT INTO lecturer VALUES ('"+tempID+"','"+newuser+"','"+newfirst+"','"+newlast+"','"+newemail+"')";
                            if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK){
                                sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                                sqlite3_step(stmt);
                                sqlite3_finalize(stmt);
                                sqlite3_close(db1);
                            }
                        } 
                    }
                    db->close();
                    cout << "New " << newrole << " " << newuser << " successfully added!" << endl;
                    return;                    
                } else  if (newrole == "student"){
                    Database *db;
                    db = new Database("Database.Assn2");
                    
                    string sqlstatement;
                    sqlite3 *db1;
                    sqlite3_stmt *stmt;
                    
                    sqlstatement = "INSERT INTO users (userName, password, role, accountStatus) VALUES ('"+newuser+"','"+newpass+"','"+newrole+"','unlocked')";
                    if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK){
                    sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                    sqlite3_step(stmt);
                    }
                    
                    vector<vector<string> > result = db->query("SELECT * FROM users;");
                    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it){
                        
                        vector<string> row = *it;   
                        string tempID;
                        if (newuser == row.at(1)){
                            
                            tempID = row.at(0);
                        
                            sqlstatement = "INSERT INTO student VALUES ('"+tempID+"','"+newuser+"','"+newfirst+"','"+newlast+"','"+newemail+"')";
                            if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK){
                                sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                                sqlite3_step(stmt);
                                sqlite3_finalize(stmt);
                                sqlite3_close(db1);
                            }
                        } 
                    }
                    db->close();
                    cout << "New " << newrole << " " << newuser << " successfully added!" << endl;
                    return;                    
                }
            }else{
                LA.adminCreateUser();
            }
        }else{
            LA.adminCreateUser();
        }
}

void Login::adminEditUser(){
    Login LA;
    Database *db;
    db = new Database("Database.Assn2");
    int choice;
    string editfirst, editlast, editemail, edituser, editID, editrole;
    cout << "Enter the username you wish to edit (type -1 to exit): ";
    cin >> edituser;
    if (edituser == "-1"){
        LA.adminInterface();
    } else {
        vector<vector<string> > result = db->query("SELECT * FROM users;");
        for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) 
        {
                vector<string> row = *it;
                if (edituser == row.at(1)) {
                    editID = row.at(0);
                    editrole = row.at(3);
                    cout << edituser << " found!" << endl;
                    cout << "What would you like to edit?" << endl;
                    cout << "   1) First name" << endl;
                    cout << "   2) Last name " << endl;
                    cout << "   3) Email" << endl;
                    cout << "   4) Edit another user" << endl;
                    cout << "   Enter your choice: ";
                    cin >> choice;
                    if(choice == 1){
                        cout << "Please enter new first name: ";
                        cin >> editfirst;
                        if(editrole == "admin"){
                            string sqlstatement;
                            sqlite3 *db1;
                            sqlite3_stmt *stmt;
                            sqlstatement = "UPDATE admin SET firstName='"+editfirst+"' WHERE userID='"+editID+"'";                     

                            if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                            {
                                  sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                                  sqlite3_step(stmt);
                                  sqlite3_finalize(stmt);
                                  sqlite3_close(db1);
                            }
                        LA.adminEditUser();
                        } 
                        if(editrole == "lecturer"){
                            string sqlstatement;
                            sqlite3 *db1;
                            sqlite3_stmt *stmt;
                            sqlstatement = "UPDATE lecturer SET firstName='"+editfirst+"' WHERE userID='"+editID+"'";                     

                            if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                            {
                                  sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                                  sqlite3_step(stmt);
                                  sqlite3_finalize(stmt);
                                  sqlite3_close(db1);
                            }
                        LA.adminEditUser();
                        }
                        if(editrole == "student"){
                            string sqlstatement;
                            sqlite3 *db1;
                            sqlite3_stmt *stmt;
                            sqlstatement = "UPDATE student SET firstName='"+editfirst+"' WHERE userID='"+editID+"'";                     

                            if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                            {
                                  sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                                  sqlite3_step(stmt);
                                  sqlite3_finalize(stmt);
                                  sqlite3_close(db1);
                            }
                        LA.adminEditUser();
                        } 
                    }
                    if(choice == 2){
                        cout << "Please enter new last name: ";
                        cin >> editlast;
                        if(editrole == "admin"){
                            string sqlstatement;
                            sqlite3 *db1;
                            sqlite3_stmt *stmt;
                            sqlstatement = "UPDATE admin SET lastName='"+editlast+"' WHERE userID='"+editID+"'";                     

                            if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                            {
                                  sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                                  sqlite3_step(stmt);
                                  sqlite3_finalize(stmt);
                                  sqlite3_close(db1);
                            }
                        LA.adminEditUser();
                        } 
                        if(editrole == "lecturer"){
                            string sqlstatement;
                            sqlite3 *db1;
                            sqlite3_stmt *stmt;
                            sqlstatement = "UPDATE lecturer SET lastName='"+editlast+"' WHERE userID='"+editID+"'";                     

                            if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                            {
                                  sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                                  sqlite3_step(stmt);
                                  sqlite3_finalize(stmt);
                                  sqlite3_close(db1);
                            }
                        LA.adminEditUser();
                        }
                        if(editrole == "student"){
                            string sqlstatement;
                            sqlite3 *db1;
                            sqlite3_stmt *stmt;
                            sqlstatement = "UPDATE student SET lastName='"+editlast+"' WHERE userID='"+editID+"'";                     

                            if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                            {
                                  sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                                  sqlite3_step(stmt);
                                  sqlite3_finalize(stmt);
                                  sqlite3_close(db1);
                            }
                        LA.adminEditUser();
                        }
                    }
                    if(choice == 3){
                        cout << "Please enter new email: ";
                        cin >> editemail;
                        if(editrole == "admin"){
                            string sqlstatement;
                            sqlite3 *db1;
                            sqlite3_stmt *stmt;
                            sqlstatement = "UPDATE admin SET email='"+editemail+"' WHERE userID='"+editID+"'";                     

                            if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                            {
                                  sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                                  sqlite3_step(stmt);
                                  sqlite3_finalize(stmt);
                                  sqlite3_close(db1);
                            }
                        LA.adminEditUser();
                        } 
                        if(editrole == "lecturer"){
                            string sqlstatement;
                            sqlite3 *db1;
                            sqlite3_stmt *stmt;
                            sqlstatement = "UPDATE lecturer SET email='"+editemail+"' WHERE userID='"+editID+"'";                     

                            if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                            {
                                  sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                                  sqlite3_step(stmt);
                                  sqlite3_finalize(stmt);
                                  sqlite3_close(db1);
                            }
                        LA.adminEditUser();
                        }
                        if(editrole == "student"){
                            string sqlstatement;
                            sqlite3 *db1;
                            sqlite3_stmt *stmt;
                            sqlstatement = "UPDATE student SET email='"+editemail+"' WHERE userID='"+editID+"'";                     

                            if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                            {
                                  sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                                  sqlite3_step(stmt);
                                  sqlite3_finalize(stmt);
                                  sqlite3_close(db1);
                            }
                        LA.adminEditUser();
                        }
                    }
                    if(choice == 4){
                        LA.adminEditUser();
                    }
                }
        }
    }
    cout << edituser << " not found! Try again!" << endl;
    LA.adminEditUser();
    db->close();
}

void Login::adminDeleteUser(){
    Login LA;
    Database *db;
    db = new Database("Database.Assn2");
    int choice;
    string editID, edituser, editrole;
    cout << "Enter the username you wish to delete (type -1 to exit): ";
    cin >> edituser;
    if (edituser == "-1"){
        LA.adminInterface();
    } else {
        vector<vector<string> > result = db->query("SELECT * FROM users;");
        for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) 
        {
                vector<string> row = *it;
                if (edituser == row.at(1)) {
                    editID = row.at(0);
                    editrole = row.at(3);
                    cout << "UserID: " << row.at(0) << endl;
                    cout << "Username: " << row.at(1) << endl;
                    cout << "Enter your choice (1 to delete, -1 to exit): ";
                    cin >> choice;
                    if (choice == -1){
                        cout << "Going back to main admin interface" << endl;
                        LA.adminInterface();
                    }
                    if (choice == 1){
                            cout << "*********THIS IS IRREVERSIBLE*********" << endl;
                            cout << "1 to confirm delete, -1 to exit" << endl;
                            cout << "Enter your choice: ";
                            cin >> choice;
                            if (choice == -1){
                                cout << "Going back to main admin interface" << endl;
                                LA.adminInterface();
                            }
                            if (choice == 1){
                                if(editrole == "admin"){
                                    string sqlstatement1;
                                    string sqlstatement2;
                                    sqlite3 *db1;
                                    sqlite3_stmt *stmt;
                                    sqlstatement1 = "DELETE FROM users WHERE userID='"+editID+"'";  
                                    sqlstatement2 = "DELETE FROM admin WHERE userID='"+editID+"'";

                                    if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                                    {
                                          sqlite3_prepare(db1, sqlstatement1.c_str(), -1, &stmt, NULL);
                                          sqlite3_step(stmt);
                                          sqlite3_finalize(stmt);
                                          sqlite3_close(db1);
                                    }
                                    if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                                    {
                                          sqlite3_prepare(db1, sqlstatement2.c_str(), -1, &stmt, NULL);
                                          sqlite3_step(stmt);
                                          sqlite3_finalize(stmt);
                                          sqlite3_close(db1);
                                    }
                                }
                                if(editrole == "lecturer"){
                                    string sqlstatement1;
                                    string sqlstatement2;
                                    sqlite3 *db1;
                                    sqlite3_stmt *stmt;
                                    sqlstatement1 = "DELETE FROM users WHERE userID='"+editID+"'";  
                                    sqlstatement2 = "DELETE FROM lecturer WHERE userID='"+editID+"'";

                                    if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                                    {
                                          sqlite3_prepare(db1, sqlstatement1.c_str(), -1, &stmt, NULL);
                                          sqlite3_step(stmt);
                                          sqlite3_finalize(stmt);
                                          sqlite3_close(db1);
                                    }
                                    if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                                    {
                                          sqlite3_prepare(db1, sqlstatement2.c_str(), -1, &stmt, NULL);
                                          sqlite3_step(stmt);
                                          sqlite3_finalize(stmt);
                                          sqlite3_close(db1);
                                    }
                                    cout << "Lecturer " << edituser << " has been deleted!" << endl;
                                    cout << "Returning to admin interface..." << endl;
                                    LA.adminInterface();
                                }    
                                if(editrole == "student"){
                                    string sqlstatement1;
                                    string sqlstatement2;
                                    sqlite3 *db1;
                                    sqlite3_stmt *stmt;
                                    sqlstatement1 = "DELETE FROM users WHERE userID='"+editID+"'";  
                                    sqlstatement2 = "DELETE FROM student WHERE userID='"+editID+"'";

                                    if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                                    {
                                          sqlite3_prepare(db1, sqlstatement1.c_str(), -1, &stmt, NULL);
                                          sqlite3_step(stmt);
                                          sqlite3_finalize(stmt);
                                          sqlite3_close(db1);
                                    }
                                    if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                                    {
                                          sqlite3_prepare(db1, sqlstatement2.c_str(), -1, &stmt, NULL);
                                          sqlite3_step(stmt);
                                          sqlite3_finalize(stmt);
                                          sqlite3_close(db1);
                                    }
                                    cout << "Student " << edituser << " has been deleted!" << endl;
                                    cout << "Returning to admin interface..." << endl;
                                    LA.adminInterface();
                                }                                                               
                            }
                        }
                 } 
            }   
        }
    cout << edituser << " not found! Try again!" << endl;
    LA.adminDeleteUser();
    db->close();
}

void Login::adminUnlockUser(){
    Login LA;
    Database *db;
    db = new Database("Database.Assn2");
    int choice;
    string unlockID, unlockuser, unlockrole;
    cout << "Enter the username you wish to unlock (type -1 to exit): ";
    cin >> unlockuser;
    if (unlockuser == "-1"){
        LA.adminInterface();
    } else {
        vector<vector<string> > result = db->query("SELECT * FROM users;");
        for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) 
        {
                vector<string> row = *it;
                unlockID = row.at(0);
                if (unlockuser == row.at(1)) {
                    if(row.at(4) == "unlocked"){
                        cout << "User " << row.at(1) << "'s account is not locked!" << endl;
                        LA.adminUnlockUser();
                    }
                    if(row.at(4) == "locked"){
                        cout << "User ID: " << row.at(0) << endl;
                        cout << "Username: " << row.at(1) << endl;
                        cout << "Enter your choice (1 to unlock, -1 to exit): ";
                        cin >> choice;
                        if (choice == -1){
                            cout << "Going back to main admin interface" << endl;
                            LA.adminInterface();
                        }
                        if (choice == 1){
                            cout << "1 to confirm, -1 to exit" << endl;
                            cout << "Enter your choice: ";
                            cin >> choice;
                            if(choice == -1){
                                cout << "Going back to main admin interface" << endl;
                                LA.adminInterface();
                            }
                            if(choice == 1){
                                
                                string sqlstatement;
                                sqlite3 *db1;
                                sqlite3_stmt *stmt;
                                sqlstatement = "UPDATE users SET accountStatus='unlocked' WHERE userID='"+unlockID+"'";    

                                if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                                {
                                      sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                                      sqlite3_step(stmt);
                                      sqlite3_finalize(stmt);
                                      sqlite3_close(db1);
                                }                               
                            }
                        }
                    }
                }   
          }
    }
    cout << unlockuser << " not found! Try again!" << endl;
    LA.adminUnlockUser(); 
    db->close();
}

void Login::adminCreateModule(){
    Login LA;
    Database *db;
    db = new Database("Database.Assn2");
    int choice;
    string moduleID, moduleName, tempModID;
    cout << "Enter new module ID: ";
    cin >> moduleID;
    cout << "Enter new module name: ";
    cin >> moduleName;
    
    vector<vector<string> > result = db->query("SELECT * FROM module;");
    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) 
    {
         vector<string> row = *it;
         
         if(moduleID != row.at(0)){
             string sqlstatement;
             sqlite3 *db1;
             sqlite3_stmt *stmt;
             sqlstatement = "INSERT INTO module VALUES ('"+moduleID+"','"+moduleName+"')";   

             if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
             {
                  sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                  sqlite3_step(stmt);
                  sqlite3_finalize(stmt);
                  sqlite3_close(db1);
             }
             cout << "1 to enter another new module, -1 to exit: " << endl;
             if(choice==1){
                 LA.adminCreateModule();
             }
             if(choice==-1){
                 LA.adminInterface();
             }            
         } else {
             cout << "Module ID: " << row.at(0) << endl;
             cout << "Module Name: " << row.at(1) << endl;
             cout << "Already exist in database! " << endl;
             cout << "1 to enter another new module, -1 to exit: ";
             cin >> choice;
             if(choice==1){
                 LA.adminCreateModule();
             }
             if(choice==-1){
                 LA.adminInterface();
             }
         }
    }
    db->close();
}

void Login::adminEditModule(){
    Login LA;
    Database *db;
    db = new Database("Database.Assn2");
    int choice;
    string moduleID, newModName;
    cout << "Enter module ID: ";
    cin >> moduleID;
    
    vector<vector<string> > result = db->query("SELECT * FROM module;");
    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) 
    {
         vector<string> row = *it;
         
         if(moduleID == row.at(0)){      
             cout << "Module ID: " << row.at(0) << endl;
             cout << "Module Name: " << row.at(1) << endl;
             cout << "Enter new module name: ";
             cin >> newModName;
             
             string sqlstatement;
             sqlite3 *db1;
             sqlite3_stmt *stmt;
             sqlstatement = "UPDATE module SET moduleName='"+newModName+"' WHERE moduleID='"+moduleID+"'";   

             if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
             {
                  sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                  sqlite3_step(stmt);
                  sqlite3_finalize(stmt);
                  sqlite3_close(db1);
             }
             cout << "1 to enter another edit module, -1 to exit: " << endl;
             if(choice==1){
                 LA.adminEditModule();
             }
             if(choice==-1){
                 LA.adminInterface();
             }            
         }
    }
    cout << "Module does not exist in database! " << endl;
    cout << "1 to enter another edit module, -1 to exit: ";
    cin >> choice;
    if(choice==1){
         LA.adminEditModule();
    }
    if(choice==-1){
         LA.adminInterface();
    }
    db->close();       
}

void Login::adminDeleteModule(){
    Login LA;
    Database *db;
    db = new Database("Database.Assn2");
    int choice;
    string moduleID;
    cout << "Enter module ID: ";
    cin >> moduleID;
    
    vector<vector<string> > result = db->query("SELECT * FROM module;");
    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) 
    {
         vector<string> row = *it;
         
         if(moduleID == row.at(0)){      
             cout << "Module ID: " << row.at(0) << endl;
             cout << "Module Name: " << row.at(1) << endl;
             cout << "Enter 1 to delete, -1 to exit: ";
             cin >> choice;
             if(choice==1){
                 cout << "*****ACTION IS IRREVERSIBLE*****" << endl;
                 cout << "Enter 1 to confirm delete, -1 to exit: ";
                 cin >> choice;
                 if(choice==1){
                    string sqlstatement;
                    sqlite3 *db1;
                    sqlite3_stmt *stmt;
                    sqlstatement = "DELETE FROM module WHERE moduleID='"+moduleID+"'";   

                    if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                    {
                         sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                         sqlite3_step(stmt);
                         sqlite3_finalize(stmt);
                         sqlite3_close(db1);
                    }
                    cout << "1 to enter another delete module, -1 to exit: ";
                    cin >> choice;
                    if(choice==1){
                        LA.adminDeleteModule();
                    }
                    if(choice==-1){
                        LA.adminInterface();
                    }
                 }if(choice==-1){
                     LA.adminInterface();
                 }
             }if(choice==-1){
                 LA.adminInterface();
             }
         }
    }
    cout << "Module does not exist in database! " << endl;
    cout << "Try again!" << endl;
    LA.adminDeleteModule();
    db->close();     
}

void Login::adminAssign(){
    Login LA;
    Database *db;
    db = new Database("Database.Assn2");
    int choice;
    string tempName, tempClassID, tempClassType, tempRCID;
    cout << "Classes that started" <<endl;
    cout << "==================================" << endl;
    vector<vector<string> > result = db->query("SELECT * FROM runningclass;");
    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) 
    {      
         vector<string> row = *it;
         cout << "Class session ID: " << row.at(0) << endl;
         cout << "Lecturer username: " << row.at(2) << endl;
         cout << "Module ID: " << row.at(3) << endl;
         if(row.at(4) == ""){
            cout << "Class ID: Unassigned" << endl;
         }else{
            cout << "Class ID: " << row.at(4) << endl; 
         }
         cout << "Class Type: " << row.at(5) << endl;
         cout << "Time: " << row.at(6) << endl;
         cout << "Date: " << row.at(7) << endl << endl;
    }
    cout << "Classes available" <<endl;
    cout << "==================================" << endl;

    vector<vector<string> > result1 = db->query("SELECT * FROM class;");
    for(vector<vector<string> >::iterator it = result1.begin(); it < result1.end(); ++it) 
    {      
        vector<string> row = *it;
        cout << "Class ID: " << row.at(0) << endl;
        cout << "Class Type: " << row.at(1) << endl;
        cout << "Class Status: " << row.at(2) << endl;
        cout << "Time " << row.at(3) << endl;
        cout << "Date: " << row.at(4) << endl << endl;
    }
    cout << "Assign Class" << endl;
    cout << "=================================" << endl;
    cout << "Enter -1 to quit" << endl;
    cout << "Enter Class session ID: ";
    cin >> tempRCID;
    if(tempRCID == "-1"){
        LA.adminInterface();
    }
    cout << "Enter Class ID: ";
    cin >> tempClassID;
    if(tempClassID == "-1"){
        LA.adminInterface();
    }
    cout << "Choose Class Type: " << endl;
    cout << "1) Lecture" << endl;
    cout << "2) Tutorial" << endl;
    cout << "3) Lab" << endl;
    cout << "Enter your choice: ";
    cin >> choice;
    if(choice == -1){
        LA.adminInterface();
    }
    if(choice==1){
        tempClassType = "lecture";
    }
    if(choice==2){
        tempClassType = "tutorial";
    }    
    if(choice==3){
        tempClassType = "lab";
    }
    vector<vector<string> > result2 = db->query("SELECT * FROM class;");
    for(vector<vector<string> >::iterator it = result2.begin(); it < result2.end(); ++it){
         vector<string> row = *it;
         if(tempClassID==row.at(0)){
             if("unavailable"==row.at(2)){
                 cout << "The class choosen is unavailable" << endl;
                 LA.adminAssign();
             }
             if("available"==row.at(2)){
                 if(tempClassType==row.at(1)){
                    string sqlstatement;
                    string sqlstatement2;
                    sqlite3 *db1;
                    sqlite3_stmt *stmt;
                    sqlstatement = "UPDATE runningclass SET classID ='"+tempClassID+"' WHERE rcID='"+tempRCID+"'";   
                    sqlstatement2 = "UPDATE class SET classStatus ='unavailable' WHERE classID ='"+tempClassID+"'";
                    if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                    {
                         sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
                         sqlite3_step(stmt);
                         sqlite3_finalize(stmt);
                         sqlite3_close(db1);
                    }
                    if(sqlite3_open(DATABASE_FILE,&db1) == SQLITE_OK)
                    {
                         sqlite3_prepare(db1, sqlstatement2.c_str(), -1, &stmt, NULL);
                         sqlite3_step(stmt);
                         sqlite3_finalize(stmt);
                         sqlite3_close(db1);
                    }
                    cout << "Class has been assigned!" << endl;
                 }else{
                     cout << "The class type does not match!" << endl;
                     LA.adminAssign();
                 }
             }
         }
    }
    db->close();     
}

void Login::adminDisplayAll(){
    Login LA;
    Database *db;
    db = new Database("Database.Assn2");  
    
    cout << left <<setw(18) << "Lecturer's name" << setw(15) << "Class venue" << setw(14) << "Class Time" << setw(14) << "Class Date" << endl;
    cout << "==========================================================================" << endl;
    vector<vector<string> > result = db->query("SELECT * FROM runningclass;");
    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) 
    {      
         vector<string> row = *it;
         cout << left <<setw(18) << row.at(2) << setw(15) << row.at(4) << setw(14) << row.at(6) << setw(14) << row.at(7) << endl;
    }
    cout << "" << endl;
    return LA.adminInterface();
}

void Login::studentInterface() {
    Login LA;

    while (1) {
       
        cout << "<<<<<<< Student Attendance System >>>>>>>" << endl;
        cout << "<<<<<<<      Student Interface      >>>>>>>" << endl << endl;
    
            int choice = 0;
            cout << "   1) Display Classes" << endl;
            cout << "   2) View Attendance" << endl;
            cout << "   3) Submit LOA" << endl;
            cout << "   4) Exit Program" << endl;
            cout << "   Please enter your choice: ";
            cin >> choice;
            cin.clear();
            cin.ignore(100, '\n'); 
            
            if (choice == 1){
                LA.displayClasses();
            }else  if (choice == 2){
                LA.studentLOA();
            }
                else if(choice==3){
                    return;
                
            } else if (choice == 4){
                cout << "Exiting Program!!!" << endl;
                exit(0);
            }
    }
}

void Login::studentLOA(){
 
    
 Database *db;
    db = new Database("Database.Assn2");
    string newuserID;                
    string sqlstatement;
    sqlite3 *db1;
    sqlite3_stmt *stmt;

    vector<vector<string> > result = db->query("SELECT * FROM attendance;");
    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it){
        
        vector<string> row = *it;
        cout<<"Please enter userID: ";
        cin>>newuserID;
        cout<<""<<endl;
        if(newuserID==row.at(1)){
            cout<<"Module ID    User ID        Status   Date    Duration        Module Grp"<<endl;
                        cout<<row.at(0)<<row.at(1)<<"   "<<row.at(2)<<"   "<<row.at(3)<<"   "<<row.at(4)<<"   "<<endl<<endl;
        }
        else{
            cout<<"Invalid User ID"<<endl<<endl;
        }
    }
        

            
}

void Login::displayClasses(){
    
    Database *db;
    db = new Database("Database.Assn2");
    string username;                
    string sqlstatement;
    sqlite3 *db1;
    sqlite3_stmt *stmt;

    vector<vector<string> > result = db->query("SELECT * FROM studentsubject;");
    for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it){
        
        vector<string> row = *it;
        cout<<"Please enter username: ";
        cin>>username;
        cout<<""<<endl;
        if(username==row.at(1)){
            cout<<"User ID      UserName        Class ID        Class Type      Module ID"<<endl;
                        cout<<row.at(1)<<"   "<<row.at(2)<<"   "<<row.at(3)<<"   "<<row.at(4)<<endl<<endl;
        }
        else{
            cout<<"No Classes to display"<<endl<<endl;
        }
    }
            
}
#endif